<!-- Modal for Cron Jobs -->
<div  id="cron" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title"> <i class="fa fa-clock-o"></i> Cron Jobs</h4>
            </div>
            <form role="form" id="cron-form">
                <div class="modal-body">
                    <div class="box-body">
                        <div class="form-group">
                            <label for="exampleInputEmail1">Order Cancel Duration</label>
                            <input class="form-control" id="orderCancelDuration"  name="orderCancelDuration" placeholder="Please Enter Order cancle Duration in Hours" type="text" data-validation="number" data-validation-error-msg="Please Enter Order cancle Duration in Hours">
                        </div>
                        <div class="form-group">
                            <label for="exampleInputEmail1">Enable Order Cancel Jobs</label>
                            <input class="form-control" id="orderCancelEnable" name="orderCancelEnable" type="checkbox" value="true">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save</button>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Modal for Notification -->
<div  id="notification" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title"> <i class="fa fa-envelope-o"></i> Notification Center</h4>
            </div>
            <form role="form" id="notification-form">
            <div class="modal-body">
                <div class="box-body">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Agent Registration Notification</label>
                        <input class="form-control" id="agentRegistrationNotification"  name="agentRegistrationNotification" placeholder="Enter email" type="text" data-validation="email" data-validation-error-msg="Please valid email.">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Order Notification</label>
                        <input class="form-control" id="orderNotification" name="orderNotification" placeholder="Enter email" type="text" data-validation="email" data-validation-error-msg="Please valid email.">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Inventory Notification</label>
                        <input class="form-control" id="inventoryNotification" name="inventoryNotification" placeholder="Enter email" type="text" data-validation="email" data-validation-error-msg="Please valid email.">
                    </div>
                    <div class="form-group">
                        <label>Order Message</label>
                        <textarea id="orderMessage" name="orderMessage" class="form-control" rows="3" placeholder="Enter Message" data-validation="required"  data-validation-error-msg="Please enter Message."></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Save</button>
            </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Modal for change password -->
<div  id="changePassword" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title"><i class="fa fa-key"></i> Account Settings</h4>
            </div>
            <div class="modal-body">
                <form role="form" id="user-account-form">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Username</label>
                        <input class="form-control" id="s-username"  name="s-username" placeholder="Enter Username" type="text" data-validation="required" data-validation-error-msg="Please username.">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Email</label>
                        <input class="form-control" id="s-email"  name="s-email" placeholder="Enter Email" type="text" data-validation="required" data-validation-error-msg="Please email.">
                    </div>
                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
                <br/>
                <hr/>
                <form role="form" id="password-change-form">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Current Password</label>
                        <input class="form-control" id="s-currentPassword"  name="s-currentPassword" placeholder="Enter Current Password" type="password" data-validation="required" data-validation-error-msg="Please valid current password.">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">New Password</label>
                        <input class="form-control" id="s-newPassword"  name="s-newPassword" placeholder="Enter Current Password" type="password" data-validation="required" data-validation-error-msg="Please valid password.">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Retype Password</label>
                        <input class="form-control" id="s-rePassword"  name="s-rePassword" placeholder="Enter Current Password" type="password" data-validation="required" data-validation-error-msg="Please valid password.">
                    </div>
                    <button type="submit" class="btn btn-primary">Save</button>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>

        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

<!-- Email Modal -->
<div class="modal fade" id="myMail" tabindex="-1" role="dialog" aria-labelledby="myMailLabel" aria-hidden="true"
     data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myMailLabel">Quick Email</h4>
            </div>
            <form id="email-form" method="post">
            <div class="modal-body">
                    <div class="form-group">
                        <input type="email" id="mailto" class="form-control" name="emailto" placeholder="Email to:" data-validation="email" data-validation-error-msg="Please valid email"/>
                    </div>
                    <div class="form-group">
                        <input type="text" id="subject" class="form-control" name="subject" placeholder="Subject" data-validation="required" data-validation-error-msg="Please Subject line."/>
                    </div>
                    <div>
                        <textarea class="editor" placeholder="Message" id="message" name="message" data-validation="required" data-validation-error-msg="Please message"
                                  style="width: 100%; height: 125px; font-size: 14px; line-height: 18px; border: 1px solid #dddddd; padding: 10px;"></textarea>
                    </div>
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-default" id="sendEmail">Send <i class="fa fa-arrow-circle-right"></i></button>
                <button type="button" class=" pull-right btn btn-default" data-dismiss="modal">Close</button>
            </div>
            </form>
        </div>
    </div>
</div>


</div>
	<script src="<?php asset("js/jquery.min.js") ?>"></script>
    <script src="<?php asset("validator/jquery.form-validator.min.js") ?>" type="text/javascript"></script>
	<script src="<?php asset("js/jquery-ui-1.10.3.min.js") ?>"	type="text/javascript"></script>
	<script src="<?php asset("js/bootstrap.min.js") ?>"	type="text/javascript"></script>
    <script src="<?php asset("dist/spin.min.js") ?>"></script>
    <script src="<?php asset("dist/ladda.min.js") ?>"></script>
	<script src="<?php asset("js/raphael-min.js") ?>"></script>
    <script src="<?php asset("js/moment.js") ?>"></script>
	<script src="<?php asset("js/plugins/morris/morris.min.js") ?>"	type="text/javascript"></script>
	<script	src="<?php asset("js/plugins/sparkline/jquery.sparkline.min.js") ?>" type="text/javascript"></script>
	<script	src="<?php asset("js/plugins/fullcalendar/fullcalendar.min.js") ?>"	type="text/javascript"></script>
	<script src="<?php asset("js/plugins/jqueryKnob/jquery.knob.js") ?>" type="text/javascript"></script>
	<script	src="<?php asset("js/plugins/daterangepicker/daterangepicker.js") ?>" type="text/javascript"></script>
	<script	src="<?php asset("js/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js") ?>" type="text/javascript"></script>
	<script src="<?php asset("js/plugins/iCheck/icheck.min.js") ?>"	type="text/javascript"></script>
	<script src="<?php asset("js/AdminLTE/app.js") ?>" type="text/javascript"></script>
	<script src="<?php asset("js/AdminLTE/dashboard.js") ?>" type="text/javascript"></script>
    <script src="<?php asset("js/jquery.uploadfile.min.js") ?>" type="text/javascript"></script>
    <script src="<?php asset("js/jquery.form.js") ?>" type="text/javascript"></script>
    <script src="<?php asset("js/messi.min.js") ?>" type="text/javascript"></script>
    <script src="<?php asset("js/jquery.bttrlazyloading.min.js")?>"></script>
    <script src="<?php asset("js/jquery.dataTables.min.js")?>" type="text/javascript"></script>


    <script src="<?php asset("js/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js")?>" type="text/javascript"></script>
    <script>
        $(function() {
            $(".editor").wysihtml5({
                "font-styles": false, //Font styling, e.g. h1, h2, etc. Default true
                "emphasis": true, //Italics, bold, etc. Default true
                "lists": true, //(Un)ordered lists, e.g. Bullets, Numbers. Default true
                "html": false, //Button which allows you to edit the generated HTML. Default false
                "link": false, //Button to insert a link. Default true
                "image": false //Button to insert an image. Default t
            });
        });
        function getElementId(element){
            var startIndex = element.indexOf("_");
            if(!isNaN(element.substr(startIndex + 1, element.length))){
                return parseInt(element.substr(startIndex + 1, element.length));
            }
            return undefined;
        }

        $("body").on("keydown", "input.number-input", function (e) {
            if (e.keyCode != 190 && e.keyCode != 13 && e.keyCode != 46 && // delete
                e.keyCode != 8 && // backspace
                (e.keyCode < 48 || e.keyCode > 57) && //numbers above keyboard
                (e.keyCode < 97 || e.keyCode > 103) // numeric keyboard numbers
                ) {
                e.preventDefault();
            }
        });

        $("body").on("keyup", "input.number-input", function (e){
            if(e.keyCode == 8 || e.keyCode == 46){
                /*if($(this).val() == ""){
                    $(this).val(0);
                }*/
            }
        });

        function errorAlert(message){
            new Messi(message, {title: 'Error', modal: true, titleClass: 'error', buttons: [{id: 0, label: 'Close', val: 'X'}]});
        }

        $(document).ready(function () {
            $("body").on("click", "button.order-mail", function () {
                $("#order-table").css('cursor', 'progress');
                $.get("getAgencyDetails/" + getElementId($(this).attr("id")),function (response) {
                    if (!response.error) {
                        $("#mailto").val(response.data.email);
                        $("#myMail").modal("show");
                    } else {
                        errorAlert(response.errorMessage);
                    }

                }).always(function () {
                        $("#order-table").css('cursor', 'pointer');
                    });

            });

            $("#notification-btn").click(function () {
                $.get('<?php ajax_url("account/notification")?>', function (response) {
                    if (!response.error) {
                        $("#agentRegistrationNotification").val(response.data.agentRegistrationEmail);
                        $("#orderNotification").val(response.data.orderPlaceEmail);
                        $("#inventoryNotification").val(response.data.inventoryReportEmail);
                        $("#orderMessage").val(response.data.orderMessage);
                        $("#notification").modal("show");
                    } else {
                        errorAlert(response.errorMessage);
                    }
                })
            });

            $("#changePassword-btn").click(function () {
                $.get('<?php ajax_url("account/accountDetails")?>', function (response) {
                    if (!response.error) {
                        $("#s-username").val(response.data.username);
                        $("#s-email").val(response.data.email);
                        $("#changePassword").modal("show");
                    } else {
                        errorAlert(response.errorMessage);
                    }
                })
            });

            $("#cron-btn").click(function(){
                $("#orderCancelEnable").prop('checked', false);
                $.get('<?php ajax_url("account/getSystemConfig")?>',function(response){
                    if(!response.error){
                        $.each(response.data, function(i, property){
                            if(property.property == "cron.order.cancel.hour"){
                               $("#orderCancelDuration").val(property.value);
                            }
                            if(property.property == "cron.order.cancel.enable" && property.value == "true"){
                                $('#orderCancelEnable').iCheck('check');
                            }
                        });
                        $("#cron").modal("show");
                    }else{
                        errorAlert(response.errorMessage);
                    }
                });
            });
        });

        $.validate({
            form: '#notification-form',
            validateOnBlur: false,
            onError: function () {
                return false;
            },
            onSuccess: function ($form) {
                $.post('<?php ajax_url("account/saveNotification")?>',$form.serialize(),function(response){
                    if(!response.error){
                        $("#notification").modal("hide");
                    }
                });
                return false;
            }
        });

        $.validate({
            form: '#user-account-form',
            validateOnBlur: false,
            onError: function () {
                return false;
            },
            onSuccess: function ($form) {
                $.post('<?php ajax_url("account/saveAccountDetails")?>',$form.serialize(),function(response){
                    if(!response.error){
                    }else{
                        errorAlert(response.errorMessage);
                    }
                });
                return false;
            }
        });

        $.validate({
            form: '#password-change-form',
            validateOnBlur: false,
            onError: function () {
                return false;
            },
            onSuccess: function ($form) {
                $.post('<?php ajax_url("account/updatePassword")?>',$form.serialize(),function(response){
                    if(!response.error){
                        $("#s-currentPassword").val("");
                        $("#s-newPassword").val("");
                        $("#s-rePassword").val("");
                    }else{
                        errorAlert(response.errorMessage);
                    }
                });
                return false;
            }
        });

        $.validate({
            form: '#email-form',
            validateOnBlur: false,
            onError: function () {
                return false;
            },
            onSuccess: function ($form) {
                $.post('<?php ajax_url("orders/mail")?>', $("#email-form").serialize(),function (response) {
                    if (response.error) {
                        errorAlert(response.errorMessage);
                    }
                }).fail(function (response) {
                        console.log(response.responseText);
                    });
                $("#mailto").val("");
                $("#subject").val("");
                $("#message").data("wysihtml5").editor.setValue("");
                return false;
            }
        });

        $.validate({
            form: '#cron-form',
            validateOnBlur:false,
            onError:function(){
                return false;
            },
            onSuccess:function($form){
                $.post('<?php ajax_url("account/saveSystemConfig")?>',$form.serialize(),function(response){
                    if(response.error){
                        errorAlert(response.errorMessage);
                    }else{
                        $("#cron").modal("hide");
                    }
                });
                return false;
            }
        });
    </script>
</body>
</html>